'use strict';
const logger = require('./log');
let tafAlarm = require("@taf/taf-alarm");

let log = logger.error;
let isAlarmOpen = true;
/**
 * 异常逻辑监听
 * @return {[type]} [description]
 */
exports.listen = function () {
    //异常捕获逻辑 Start //
    process.on('uncaughtException', err => {
        log.error('uncaughtException', err.message + '\n' + err.stack);
        if (isAlarmOpen) {
            tafAlarm.TAF_ALARM('uncaughtException', err.message + '\n' + err.stack);
        }
    });

    process.on('unhandledRejection', (reason, p) => {
        log.error("Unhandled Rejection at: Promise ", p, " reason: ", reason);
        if (isAlarmOpen) {
            tafAlarm.TAF_ALARM('unhandledRejection', p + " reason: " + reason);
        }
    });

    process.on('rejectionHandled', (reason, p) => {
        log.error("Rejection Unhandled at: Promise ", p, " reason: ", reason);
        if (isAlarmOpen) {
            tafAlarm.TAF_ALARM('rejectionHandled', p + " reason: " + reason);
        }
    });
    //异常捕获逻辑 End //
};